﻿using System;
using System.Linq;
using System.Reflection;
using Axial.jQuery;

namespace Axial
{
	public static partial class CallHandler
	{
		[CallHandler(typeof(JQuery))]
		private static string HandleJQuery(MethodInfo method, object called, object[] stack)
		{
			if (method.Name == "jQuery")
				return string.Format("$({0})", stack[0]);
			if (method.Name == "get_Document")
				return "$(document)";
			if (method.Name == "get_Window")
				return "$(window)";
			if (method.Name == "get_This")
				return "$(this)";
			if (method.Name == "get")
				return string.Format("$.get({0}, {1}, {2})", stack[0], stack[1], JSWebTranslator.DissembleAction(stack[2]));
			return null;
		}

		[CallHandler(typeof(JQueryVar))]
		private static string HandleJQueryVar(MethodInfo method, object called, object[] stack)
		{
			foreach (int i in Enumerable.Range(0, stack.Length))
				if (stack[i] is Delegate)
					if (((stack[i] as Delegate).Method.Attributes & MethodAttributes.Family) == MethodAttributes.Family)
						stack[i] = (stack[i] as Delegate).Method.Name;
					else
						stack[i] = JSWebTranslator.DissembleAction(stack[i]);
			return StandardHandler(method, called, stack);
		}

		[CallHandler(typeof(Offset))]
		private static string HandleOffset(MethodInfo method, object called, object[] stack)
		{
			return StandardHandler(method, called, stack);
		}
	}
}
